home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / cc1.spur / stupid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  13.7 KB  |  460 lines

  1. /* Dummy data flow analysis for GNU compiler in nonoptimizing mode.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* This file performs stupid register allocation, which is used
  23.    when cc1 gets the -noreg switch (which is when cc does not get -O).
  24.  
  25.    Stupid register allocation goes in place of the the flow_analysis,
  26.    local_alloc and global_alloc passes.  combine_instructions cannot
  27.    be done with stupid allocation because the data flow info that it needs
  28.    is not computed here.
  29.  
  30.    In stupid allocation, the only user-defined variables that can
  31.    go in registers are those declared "register".  They are assumed
  32.    to have a life span equal to their scope.  Other user variables
  33.    are given stack slots in the rtl-generation pass and are not
  34.    represented as pseudo regs.  A compiler-generated temporary
  35.    is assumed to live from its first mention to its last mention.
  36.  
  37.    Since each pseudo-reg's life span is just an interval, it can be
  38.    represented as a pair of numbers, each of which identifies an insn by
  39.    its position in the function (number of insns before it).  The first
  40.    thing done for stupid allocation is to compute such a number for each
  41.    insn.  It is called the suid.  Then the life-interval of each
  42.    pseudo reg is computed.  Then the pseudo regs are ordered by priority
  43.    and assigned hard regs in priority order.  */
  44.  
  45. #include <stdio.h>
  46. #include "config.h"
  47. #include "rtl.h"
  48. #include "hard-reg-set.h"
  49. #include "regs.h"
  50. #include "alloca.h"
  51.  
  52. /* Vector mapping INSN_UIDs to suids.
  53.    The suids are like uids but increase monononically always.
  54.    We use them to see whether a subroutine call came
  55.    between a variable's birth and its death.  */
  56.  
  57. static short *uid_suid;
  58.  
  59. /* Get the suid of an insn.  */
  60.  
  61. #define INSN_SUID(INSN) (uid_suid[INSN_UID (INSN)])
  62.  
  63. /* Record the suid of the last CALL_INSN
  64.    so we can tell whether a potential combination crosses any calls.  */
  65.  
  66. static int last_call_suid;
  67.  
  68. /* Element N is suid of insn where life span of pseudo reg N ends.
  69.    Element is  0 if register N has not been seen yet on backward scan.  */
  70.  
  71. static short *reg_where_dead;
  72.  
  73. /* Element N is suid of insn where life span of pseudo reg N begins.  */
  74.  
  75. static short *reg_where_born;
  76.  
  77. /* Numbers of pseudo-regs to be allocated, highest priority first.  */
  78.  
  79. static short *reg_order;
  80.  
  81. /* Indexed by reg number (hard or pseudo), nonzero if register is live
  82.    at the current point in the instruction stream.  */
  83.  
  84. static char *regs_live;
  85.  
  86. /* Indexed by insn's suid, the set of hard regs live after that insn.  */
  87.  
  88. static HARD_REG_SET *after_insn_hard_regs;
  89.  
  90. /* Record that hard reg REGNO is live after insn INSN.  */
  91.  
  92. #define MARK_LIVE_AFTER(INSN,REGNO)  \
  93.   SET_HARD_REG_BIT (after_insn_hard_regs[INSN_SUID (INSN)], (REGNO))
  94.  
  95. static void stupid_mark_refs ();
  96. static int stupid_reg_compare ();
  97. static int stupid_find_reg ();
  98.  
  99. /* Stupid life analysis is for the case where only variables declared
  100.    `register' go in registers.  For this case, we mark all
  101.    pseudo-registers that belong to register variables as
  102.    dying in the last instruction of the function, and all other
  103.    pseudo registers as dying in the last place they are referenced.
  104.    Hard registers are marked as dying in the last reference before
  105.    the end or before each store into them.  */
  106.  
  107. void
  108. stupid_life_analysis (f, nregs, file)
  109.      rtx f;
  110.      int nregs;
  111.      FILE *file;
  112. {
  113.   register int i;
  114.   register rtx last, insn;
  115.   int max_uid;
  116.  
  117.   bzero (regs_ever_live, sizeof regs_ever_live);
  118.  
  119.   regs_live = (char *) alloca (nregs);
  120.  
  121.   /* First find the last real insn, and count the number of insns,
  122.      and assign insns their suids.  */
  123.  
  124.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  125.     if (INSN_UID (insn) > i)
  126.       i = INSN_UID (insn);
  127.  
  128.   max_uid = i + 1;
  129.   uid_suid = (short *) alloca ((i + 1) * sizeof (short));
  130.  
  131.   /* Compute the mapping from uids to suids.
  132.      Suids are numbers assigned to insns, like uids,
  133.      except that suids increase monotonically through the code.  */
  134.  
  135.   last = 0;            /* In case of empty function body */
  136.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  137.     {
  138.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  139.       || GET_CODE (insn) == JUMP_INSN)
  140.     last = insn;
  141.       INSN_SUID (insn) = ++i;
  142.     }
  143.  
  144.   last_call_suid = 0;
  145.  
  146.   max_regno = nregs;
  147.  
  148.   /* Allocate tables to record info about regs.  */
  149.  
  150.   reg_where_dead = (short *) alloca (nregs * sizeof (short));
  151.   bzero (reg_where_dead, nregs * sizeof (short));
  152.  
  153.   reg_where_born = (short *) alloca (nregs * sizeof (short));
  154.   bzero (reg_where_born, nregs * sizeof (short));
  155.  
  156.   reg_order = (short *) alloca (nregs * sizeof (short));
  157.   bzero (reg_order, nregs * sizeof (short));
  158.  
  159.   reg_renumber = (short *) oballoc (nregs * sizeof (short));
  160.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  161.     reg_renumber[i] = i;
  162.  
  163.   after_insn_hard_regs = (HARD_REG_SET *) alloca (max_uid * sizeof (HARD_REG_SET));
  164.   bzero (after_insn_hard_regs, max_uid * sizeof (HARD_REG_SET));
  165.  
  166.   /* Allocate and zero out many data structures
  167.      that will record the data from lifetime analysis.  */
  168.  
  169.   allocate_for_life_analysis ();
  170.  
  171.   for (i = 0; i < max_regno; i++)
  172.     {
  173.       reg_n_deaths[i] = 1;
  174.     }
  175.  
  176.   bzero (regs_live, nregs);
  177.  
  178.   /* Find where each pseudo register is born and dies,
  179.      by scanning all insns from the end to the start
  180.      and noting all mentions of the registers.
  181.  
  182.      Also find where each hard register is live
  183.      and record that info in after_insn_hard_regs.
  184.      regs_live[I] is 1 if hard reg I is live
  185.      at the current point in the scan.  */
  186.  
  187.   for (insn = last; insn; insn = PREV_INSN (insn))
  188.     {
  189.       register HARD_REG_SET *p = after_insn_hard_regs + INSN_SUID (insn);
  190.  
  191.       /* Copy the info in regs_live
  192.      into the element of after_insn_hard_regs
  193.      for the current position in the rtl code.  */
  194.  
  195.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  196.     if (regs_live[i])
  197.       SET_HARD_REG_BIT (*p, i);
  198.  
  199.       /* Mark all call-clobbered regs as live after each call insn
  200.      so that a pseudo whose life span includes this insn
  201.      will not go in one of them.
  202.      Then mark those regs as all dead for the continuing scan
  203.      of the insns before the call.  */
  204.  
  205.       if (GET_CODE (insn) == CALL_INSN)
  206.     {
  207.       last_call_suid = INSN_SUID (insn);
  208.       IOR_HARD_REG_SET (after_insn_hard_regs[last_call_suid],
  209.                 call_used_reg_set);
  210.       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  211.         if (call_used_regs[i])
  212.           regs_live[i] = 0;
  213.     }
  214.  
  215.       /* Update which hard regs are currently live
  216.      and also the birth and death suids of pseudo regs
  217.      based on the pattern of this insn.  */
  218.  
  219.       if (GET_CODE (insn) == INSN
  220.       || GET_CODE (insn) == CALL_INSN
  221.       || GET_CODE (insn) == JUMP_INSN)
  222.     {
  223.       stupid_mark_refs (PATTERN (insn), insn);
  224.     }
  225.     }
  226.  
  227.   /* Now decide the order in which to allocate the pseudo registers.  */
  228.  
  229.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  230.     reg_order[i] = i;
  231.  
  232.   qsort (®_order[FIRST_PSEUDO_REGISTER],
  233.      max_regno - FIRST_PSEUDO_REGISTER, sizeof (short),
  234.      stupid_reg_compare);
  235.  
  236.   /* Now, in that order, try to find hard registers for those pseudo regs.  */
  237.  
  238.   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
  239.     {
  240.       register int r = reg_order[i];
  241.       enum reg_class class;
  242.  
  243.       /* Now find the best hard-register class for this pseudo register */
  244.       if (N_REG_CLASSES > 1)
  245.     class = reg_preferred_class (r);
  246.  
  247.       reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], class,
  248.                      PSEUDO_REGNO_MODE (r),
  249.                      reg_where_born[r],
  250.                      reg_where_dead[r]);
  251.  
  252.       /* If no reg available in that class,
  253.      try any reg.  */
  254.       if (reg_renumber[r] == -1)
  255.     reg_renumber[r] = stupid_find_reg (reg_crosses_call[r], GENERAL_REGS,
  256.                        PSEUDO_REGNO_MODE (r),
  257.                        reg_where_born[r],
  258.                        reg_where_dead[r]);
  259.     }
  260.  
  261.   if (file)
  262.     dump_flow_info (file);
  263. }
  264.  
  265. /* Comparison function for qsort.
  266.    Returns -1 (1) if register *R1P is higher priority than *R2P.  */
  267.  
  268. static int
  269. stupid_reg_compare (r1p, r2p)
  270.      short *r1p, *r2p;
  271. {
  272.   register int r1 = *r1p, r2 = *r2p;
  273.   register int len1 = reg_where_dead[r1] - reg_where_born[r1];
  274.   register int len2 = reg_where_dead[r2] - reg_where_born[r2];
  275.  
  276.   if (len1 != len2)
  277.     return len2 - len1;
  278.  
  279.   return reg_n_refs[r1] - reg_n_refs[r2];
  280. }
  281.  
  282. /* Find a block of SIZE words of hard registers in reg_class CLASS
  283.    that can hold a value of machine-mode MODE
  284.      (but actually we test only the first of the block for holding MODE)
  285.    currently free from after insn whose suid is BIRTH
  286.    through the insn whose suid is DEATH,
  287.    and return the number of the first of them.
  288.    Return -1 if such a block cannot be found.
  289.    If CALL_PRESERVED is nonzero, insist on registers preserved
  290.    over subroutine calls, and return -1 if cannot find such.  */
  291.  
  292. static int
  293. stupid_find_reg (call_preserved, class, mode,
  294.          born_insn, dead_insn)
  295.      int call_preserved;
  296.      enum reg_class class;
  297.      enum machine_mode mode;
  298.      int born_insn, dead_insn;
  299. {
  300.   register int i, ins;
  301. #ifdef HARD_REG_SET
  302.   register        /* Declare them register if they are scalars.  */
  303. #endif
  304.     HARD_REG_SET used, this_reg;
  305.  
  306.   COPY_HARD_REG_SET (used,
  307.              call_preserved ? call_used_reg_set : fixed_reg_set);
  308.  
  309.   /* BEGINNING OF PATCH */
  310.   if (born_insn == dead_insn) 
  311.     IOR_HARD_REG_SET (used, after_insn_hard_regs[born_insn]);
  312.   /* END OF PATCH */
  313.   for (ins = born_insn; ins < dead_insn; ins++)
  314.     IOR_HARD_REG_SET (used, after_insn_hard_regs[ins]);
  315.  
  316.   IOR_COMPL_HARD_REG_SET (used, reg_class_contents[(int) class]);
  317.  
  318.   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  319.     {
  320. #ifdef REG_ALLOC_ORDER
  321.       int regno = reg_alloc_order[i];
  322. #else
  323.       int regno = i;
  324. #endif
  325.       if (! TEST_HARD_REG_BIT (used, regno)
  326.       && HARD_REGNO_MODE_OK (regno, mode))
  327.     {
  328.       register int j;
  329.       register int size1 = HARD_REGNO_NREGS (regno, mode);
  330.       for (j = 1; j < size1 && ! TEST_HARD_REG_BIT (used, regno + j); j++);
  331.       if (j == size1)
  332.         {
  333.           CLEAR_HARD_REG_SET (this_reg);
  334.           while (--j >= 0)
  335.         SET_HARD_REG_BIT (this_reg, regno + j);
  336.           for (ins = born_insn; ins < dead_insn; ins++)
  337.         {
  338.           IOR_HARD_REG_SET (after_insn_hard_regs[ins], this_reg);
  339.         }
  340.           return regno;
  341.         }
  342. #ifndef REG_ALLOC_ORDER
  343.       regno += j;            /* Skip starting points we know will lose */
  344. #endif
  345.     }
  346.     }
  347.   return -1;
  348. }
  349.  
  350. /* Walk X, noting all assignments and references to registers
  351.    and recording what they imply about life spans.
  352.    INSN is the current insn, supplied so we can find its suid.  */
  353.  
  354. static void
  355. stupid_mark_refs (x, insn)
  356.      rtx x, insn;
  357. {
  358.   register RTX_CODE code = GET_CODE (x);
  359.   register char *fmt;
  360.   register int regno, i;
  361.  
  362.   if (code == SET || code == CLOBBER)
  363.     {
  364.       if (SET_DEST (x) != 0 && GET_CODE (SET_DEST (x)) == REG)
  365.     {
  366.       /* Register is being assigned.  */
  367.       regno = REGNO (SET_DEST (x));
  368.  
  369.       /* For hard regs, update the where-live info.  */
  370.       if (regno < FIRST_PSEUDO_REGISTER)
  371.         {
  372.           register int j
  373.         = HARD_REGNO_NREGS (regno, GET_MODE (SET_DEST (x)));
  374.           while (--j >= 0)
  375.         {
  376.           regs_ever_live[regno+j] = 1;
  377.           regs_live[regno+j] = 0;
  378.           /* The following line is for unused outputs;
  379.              they do get stored even though never used again.  */
  380.           MARK_LIVE_AFTER (insn, regno);
  381.         }
  382.         }
  383.       /* For pseudo regs, record where born, where dead, number of
  384.          times used, and whether live across a call.  */
  385.       else
  386.         {
  387.           /* Update the life-interval bounds of this reg.  */
  388.           reg_where_born[regno] = INSN_SUID (insn);
  389.  
  390.           /* The reg must live at least one insn even
  391.          if it is never again used--because it has to go
  392.          in SOME hard reg.  */
  393.           if (reg_where_dead[regno] < INSN_SUID (insn) + 1)
  394.         reg_where_dead[regno] = INSN_SUID (insn) + 1;
  395.  
  396.           /* Count the refs of this reg.  */
  397.           reg_n_refs[regno]++;
  398.  
  399.           if (last_call_suid < reg_where_dead[regno])
  400.         reg_crosses_call[regno] = 1;
  401.         }
  402.     }
  403.       /* Record references from the value being set,
  404.      or from addresses in the place being set if that's not a reg.
  405.      If setting a SUBREG, we treat the entire reg as *used*.  */
  406.       if (code == SET)
  407.     {
  408.       stupid_mark_refs (SET_SRC (x), insn);
  409.       if (GET_CODE (SET_DEST (x)) != REG)
  410.         stupid_mark_refs (SET_DEST (x), insn);
  411.     }
  412.       return;
  413.     }
  414.  
  415.   /* Register value being used, not set.  */
  416.  
  417.   if (code == REG)
  418.     {
  419.       regno = REGNO (x);
  420.       if (regno < FIRST_PSEUDO_REGISTER)
  421.     {
  422.       /* Hard reg: mark it live for continuing scan of previous insns.  */
  423.       register int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  424.       while (--j >= 0)
  425.         {
  426.           regs_ever_live[regno+j] = 1;
  427.           regs_live[regno+j] = 1;
  428.         }
  429.     }
  430.       else
  431.     {
  432.       /* Pseudo reg: record first use, last use and number of uses.  */
  433.  
  434.       reg_where_born[regno] = INSN_SUID (insn);
  435.       reg_n_refs[regno]++;
  436.       if (regs_live[regno] == 0)
  437.         {
  438.           regs_live[regno] = 1;
  439.           reg_where_dead[regno] = INSN_SUID (insn);
  440.         }
  441.     }
  442.       return;
  443.     }
  444.  
  445.   /* Recursive scan of all other rtx's.  */
  446.  
  447.   fmt = GET_RTX_FORMAT (code);
  448.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  449.     {
  450.       if (fmt[i] == 'e')
  451.     stupid_mark_refs (XEXP (x, i), insn);
  452.       if (fmt[i] == 'E')
  453.     {
  454.       register int j;
  455.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  456.         stupid_mark_refs (XVECEXP (x, i, j), insn);
  457.     }
  458.     }
  459. }
  460.